/* eslint-disable @typescript-eslint/ban-types */
/* eslint-disable prefer-promise-reject-errors */
import axios from 'axios';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';

export interface axiosError {
  message: string
  code: string
}

const router = useRouter();

const source = axios.CancelToken.source();

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const createAxiosHttp = (_config:any = {}, baseURL = MYCONF.BASE_URL) => {
  const axiosParams = {
    // headers: {
    //     'X-Requested-With': 'XMLHttpRequest',
    //     'Cache-Control': 'no-cache',
    //     'Content-Type': 'application/json;charset=utf-8',
    //     Accept: 'application/json;version=3.0;compress=false;',
    // },
    ..._config,
    timeout: 500000,
    baseURL,
  };

  const fetch = axios.create(axiosParams);
  fetch.interceptors.request.use(
    (config:any) => {
      // 跳过登录接口
      if (!config?.url.includes('login')) {
        const token = sessionStorage.getItem('token');
        if (token) {
          config.headers.Authorization = `Bearer ${token}`;
        } else {
          ElMessage.warning('登录状态失效，请重新登录');
          router.push('/login');
        }
      }
      config.headers = {
        ...config.headers,
        'content-type': 'application/json',
      };
      return config;
    },
    (error) => Promise.reject(error),
  );
  fetch.interceptors.response.use(
    (response) => {
      if (response.status === 200) {
        const { data } = response;
        const { error } = data || {};

        // if (data.success) {
        //   return data;
        // }
        if (data.status === 0) {
          return data;
        }
        if (error) {
          source.cancel();
          return Promise.reject({
            message: error?.message || '',
            code: '',
          });
        }
        return data.data;
      }
      return response;
    },
    (error: any):Promise<axiosError> => {
      const defaultMessage = '网络错误 稍后再试';
      // eslint-disable-next-line no-underscore-dangle
      // if (error.__CANCEL__) {
      //   return ;
      // }
      return Promise.reject({
        message: (error && error.message) || defaultMessage,
        code: '5000',
      });
    },
  );
  return fetch;
};
export default createAxiosHttp();
